
package com.idealighter.game.games.fqzs;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.dictionary.domain.SharkRoomDomain;
import com.idealighter.game.games.fqzs.message.ReqEnterGameHallMsg;
import com.idealighter.game.games.fqzs.message.ReqEnterRoomMsg;
import com.idealighter.game.games.fqzs.message.ReqFastEnterTableMsg;
import com.idealighter.game.robot.core.Player;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.concurrent.TimeUnit;

/**
 * 飞禽走兽机器人.
 */
public class SharkPlayer extends Player {
  // 庄家最小筹码
  public final long bankerMinChips;
  // 最大机器人申请上庄家人数
  public final int maxRobotApplyBankers;
  // 最大申请上庄人数
  public final int maxApplyBankers;
  // 申请坐庄的概率(x/100)
  public final int applyBankerProb = 40;

  public SharkRoomDomain roomDomain;

  /**
   * 构造函数.
   * 
   * @param playerId .
   * @param userName .
   * @param roomId .
   * @param initGold .
   * @param totalGameNum .
   * @param tableGameNum .
   */
  public SharkPlayer(long playerId, String userName, int roomId, int initGold, int totalGameNum,
      int tableGameNum, RobotConfigDomain configDomain, SharkRoomDomain sharkRoomDomain) {
    super(playerId, userName, roomId, initGold, totalGameNum, tableGameNum, configDomain);

    bankerMinChips = configDomain.getBeBankerChips();
    maxRobotApplyBankers = configDomain.getRobotBankerApplyNums();
    maxApplyBankers = configDomain.getBankerApplyNums();

    this.roomDomain = sharkRoomDomain;
  }

  @Override
  public boolean goldCheck() {
    long gold = playerInfo.getGold();
    if (gold < roomDomain.getLower()) {
      LOG.info("[飞禽走兽]机器人[{}][{}]金币小于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    long upper = roomDomain.getUpper();
    if (upper > 0L && gold > upper) {
      LOG.info("[飞禽走兽]机器人[{}][{}]金币高于房间上限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold < roomDomain.getAfee()) {
      LOG.info("[飞禽走兽]机器人[{}][{}]金币小于台费退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    return true;
  }

  @Override
  public void enterGameHall() {
    sendMsg(new ReqEnterGameHallMsg());
  }

  @Override
  public void enterRoom() {
    if (rooms.contains(roomId)) {
      if (!goldCheck()) {
        return;
      }
      ReqEnterRoomMsg msg = new ReqEnterRoomMsg();
      msg.setRoomId(roomId);
      sendMsg(msg);
    }
  }

  @Override
  public void enterTable() {
    if (!goldCheck()) {
      return;
    }
    int roomId = this.roomId;
    if (roomId > 0) {
      ReqFastEnterTableMsg msg = new ReqFastEnterTableMsg();
      msg.setRoomId(roomId);

      schedule(() -> sendMsg(msg), RandCodeUtil.random(3, 5), TimeUnit.SECONDS);
    }
  }

  @Override
  public Game game() {
    return Game.FQZS;
  }

  @Override
  public boolean enterPwdTableAble() {
    return true;
  }

}
